__fzf_history__() {
	local output
	output=$(
		set +o pipefail
		builtin fc -lnr -1024 | sed 's/^[ \t]*//g' |
			fzf --height 40% --layout=reverse --border --scheme=history --query "$READLINE_LINE"
	) || return
	READLINE_LINE=${output}
	if [[ -z "$READLINE_POINT" ]]; then
		echo "$READLINE_LINE"
	else
		READLINE_POINT=0x7fffffff
	fi
}

bind -m emacs-standard -x '"\C-r": __fzf_history__'
bind -m vi-command -x '"\C-r": __fzf_history__'
bind -m vi-insert -x '"\C-r": __fzf_history__'
